﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using System.Data.Linq;
using System.Configuration;
using System.Diagnostics;
using DomainModel.Entities;

namespace DomainModel.Concrete
{
    public class VUserRepository:IVUserRepository
    {
        private Table<vUser> vusersTable;
        private DataContext dataContext;

        public VUserRepository()
        {
            
            String cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
            dataContext = new DataContext(cs);
            vusersTable = dataContext.GetTable<vUser>();
        }

        public IQueryable<vUser> Users
        {
            get { return vusersTable; }
        }

        public vUser GetUser(Int32 id)
        {
            try
            {
                return vusersTable.First(d => d.UserID == id);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public vUser GetUser(string login)
        {
            try
            {
                return vusersTable.First(d => d.Login == login);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public IQueryable<vUser> GetUsersOrdered()
        {
            return vusersTable.OrderBy(usr => usr.Surname);
        }
       
        public void RefreshContext()
        {
           vusersTable.Context.Refresh(RefreshMode.OverwriteCurrentValues, vusersTable);        
        }
    }
}
